package com.atguigu.edu.service.impl;

import com.atguigu.edu.entity.Chapter;
import com.atguigu.edu.entity.Course;
import com.atguigu.edu.entity.CourseDescription;
import com.atguigu.edu.entity.Video;
import com.atguigu.edu.entity.vo.CourseInfoVo;
import com.atguigu.edu.entity.vo.CoursePublishVo;
import com.atguigu.edu.entity.vo.front.CourseWebVo;
import com.atguigu.edu.mapper.CourseMapper;
import com.atguigu.edu.service.ChapterService;
import com.atguigu.edu.service.CourseDescriptionService;
import com.atguigu.edu.service.CourseService;
import com.atguigu.edu.service.VideoService;
import com.atguigu.serviceBash.GlobalException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-06
 */
@Service
@Slf4j
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    /**
     * 添加课程分类
     *
     * @param courseInfoVo
     * @return
     */
    @SuppressWarnings("all")
    @Autowired
    CourseDescriptionService courseDescriptionService;

    @Override
    public String addCourse(CourseInfoVo courseInfoVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo, course);
//        添加 courese
        try {
            int i1 = baseMapper.insert(course);
            if (i1 == 0) {
                throw new GlobalException("Course失败!", 20001);
            }
        } catch (Exception e) {
            log.error("Course添加失败");
            e.printStackTrace();
            return null;
        }

        CourseDescription courseDescription = new CourseDescription();
        BeanUtils.copyProperties(courseInfoVo, courseDescription);
//        获取添加的id 赋值给描述表中的id
        String id = course.getId();
        courseDescription.setId(id);
        try {
            boolean i2 = courseDescriptionService.save(courseDescription);
            if (!i2) {
                throw new GlobalException("Description失败!", 20001);
            }
        } catch (Exception e) {
            log.error("Course添加失败");
            e.printStackTrace();
            return null;
        }
        return course.getId();
    }

    /**
     * 根据id查询课程信息 做回显用
     *
     * @param courseId
     * @return
     */
    @Override
    public CourseInfoVo getCourseOne(String courseId) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("id", courseId);
        Course course = baseMapper.selectOne(wrapper);
        wrapper = null;

        QueryWrapper<CourseDescription> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("id", courseId);
        CourseDescription courseDescription = courseDescriptionService.getOne(wrapper1);
        wrapper1 = null;

        CourseInfoVo courseInfoVo = new CourseInfoVo();
        if (course != null) {
            BeanUtils.copyProperties(course, courseInfoVo);
        }
        if (courseDescription != null) {
            courseInfoVo.setDescription(courseDescription.getDescription());
        }
        return courseInfoVo;
    }

    /**
     * 修改course 和courseDescription 的实现
     *
     * @param courseInfoVo
     * @return
     */
    @Override
    public boolean updateCourseAndCourseDescriptionsById(CourseInfoVo courseInfoVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo, course);
//        记录
        int i = -1;
        try {
            i = baseMapper.updateById(course);
            if (!(i > 0)) {
                throw new GlobalException("修改失败,该课程标题可能已经存在!", 20001);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        CourseDescription courseDescription = new CourseDescription();
        BeanUtils.copyProperties(courseInfoVo, courseDescription);
        boolean b = false;
        try {
            b = courseDescriptionService.updateById(courseDescription);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
//        无异常返回结果
        return true;
    }

    /**
     * 返回最终发布对象
     */

    @Override
    public CoursePublishVo getPublishById(String courseId) {
        CoursePublishVo coursePublishVo = baseMapper.getPublishById(courseId);
        return coursePublishVo;
    }

    @Autowired
    ChapterService chapterService;
    //    已注入
//    @Autowired
//    CourseDescriptionService courseDescriptionService;
    @Autowired
    VideoService videoService;

    @Override
    public boolean deleteCourseById(String courseId) {
        try {
            //        删除课程描述
            courseDescriptionService.removeById(courseId);
//        删除课程章节
            QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
            wrapper.eq("course_Id", courseId);
            chapterService.remove(wrapper);
            wrapper = null;
//      删除小节
            QueryWrapper<Video> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("course_Id", courseId);
            videoService.remove(wrapper1);
            wrapper1 = null;

//      删除课程
            int i = baseMapper.deleteById(courseId);
            boolean b4 = i > 0;
            if (!b4) {
                throw new GlobalException("错误!", 20001);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    //    @Cacheable("","")
    @Override
    public List<Course> getCourseByLimit8() {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("view_count");
        wrapper.last("limit 0,8");
        List<Course> list = baseMapper.selectList(wrapper);
        wrapper = null;
        return list;
    }

    //根据课程id，编写sql语句查询课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }


}
